package com.wondersgroup.fjzlpt.org.controller.view.jtqy;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.quick.framework.util.QuickPrincipalUtils;
import com.wondersgroup.fjzlpt.org.dao.inter.jtqy.QysjbDao;
import com.wondersgroup.fjzlpt.org.util.ControllerCommonUtil;
import com.wondersgroup.fjzlpt.org.service.inter.gzt.GztService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.web.utils.RequestUtils;
import com.quick.framework.boot.web.wapper.QuickPrincipal;
import com.wondersgroup.fjzlpt.org.dao.inter.fwbFwxx.FwbFwxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jtqy.QyxxDAO;
import com.wondersgroup.fjzlpt.org.service.inter.fwb.FwbService;
import com.wondersgroup.fjzlpt.org.service.inter.jtqy.JtqyService;

@Controller
@RequestMapping("/jtqy")
public class JtqyController {
	
	@Autowired
	private JtqyService jtqyService;
	
	@Autowired
	private FwbService fwbService;
	
	@Autowired
	private FwbFwxxDAO fwbFwxxDAO;

	@Autowired
	private QyxxDAO qyxxDao;

	@Autowired
	private QysjbDao qysjbDao;

	@Value("${common.WSGLJGLBDM}")
	private String GLJGLBDM;

	/**
	 * 正式签约页面

	 * @param request
	 * @return
	 */
	@RequestMapping("/index.html")
	public String index_jtqy(HttpServletRequest request) {
        String USERID = QuickPrincipalUtils.getUserID(request);
		String PTJGID = QuickPrincipalUtils.getPtjgID(request);
		String JGXZQHDM = QuickPrincipalUtils.getXzqhdm(request);
		Map map = RequestUtils.GetParameterMap(request);
		map.put("JGXZQHDM", JGXZQHDM);
		map.put("USERID", USERID);
		map.put("YLJGID", PTJGID);
		map.put("FWBZT", "1");

		//团队长查询所拥有的团队，普通医生查询所在团队
		if(!ControllerCommonUtil.ysRole.equals(QuickPrincipalUtils.getRoleID(request))){
			map.put("TDJS", "1");
		}
		map.put("TDZT", "1");
        JSONArray tdAry = jtqyService.queryTdxxAll(map);
        JSONArray fwbAry = fwbService.queryAll(map);
        JSONObject data = new JSONObject();
        data.put("tdAry", tdAry);
        data.put("fwbAry", fwbAry);
        request.setAttribute("data", data);
		return "jtqy/index";
	}
    /**
     * 新增家庭签约页面
     * @param request
     * @return
     */
	@RequestMapping("/indexXz.html")
	public String index_xzjtqy(HttpServletRequest request) {
		return "jtqy/indexXz";
	}
	
    /**
     * 预签约页面
     * @param request
     * @return
     */
	@RequestMapping("/indexYqy.html")
	public String index_yqy(HttpServletRequest request) {
		Map map = ControllerCommonUtil.getQueryTdxxParams(request);
		map.put("TDJS","1");
		JSONArray TDXX = jtqyService.queryTdxxAll(map);
		request.setAttribute("tdxxArr", TDXX);
		return "jtqy/indexYqy";
	}
	
    /**
     * 转介审核页面
     * @param request
     * @return
     */
	@RequestMapping("/indexZjsh.html")
	public String index_zjsh(HttpServletRequest request) {
		Map map = ControllerCommonUtil.getQueryTdxxParams(request);
		map.put("TDJS","1");
		JSONArray TDXX = jtqyService.queryTdxxAll(map);
		request.setAttribute("tdxxAry", TDXX);
		return "jtqy/indexZjsh";
	}
	
	/**
	 * 签约详情页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/manager.html")
	public String manager_jtqy(HttpServletRequest request) {
		JSONObject requestData = new JSONObject();
		Map<String,String> map = RequestUtils.GetParameterMap(request);
		requestData.put("PTJGID", QuickPrincipalUtils.getPtjgID(request));
		requestData.put("YLJGMC", QuickPrincipalUtils.getUserinfoByKey(request,"YLJGMC"));
		requestData.put("USERID", QuickPrincipalUtils.getUserID(request));
		requestData.put("XM", QuickPrincipalUtils.getUserinfoByKey(request,"XM"));
		map.put("JGXZQHDM", QuickPrincipalUtils.getXzqhdm(request));
		map.put("YLJGID", QuickPrincipalUtils.getPtjgID(request));
		map.put("USERID", QuickPrincipalUtils.getUserID(request));
		map.put("FWBZT", "1");
		map.put("TDZT", "1");
		/*是否加载页面缓存数据*/
		boolean loadCache = false;
		if(map.containsKey("loadCache")){
			map.remove("loadCache");
			loadCache = true;
			requestData.putAll(jtqyService.queryManagerInitData(map));
		}else{
			requestData.putAll(jtqyService.findQyxxById(map));
		}
		requestData.put("loadCache", loadCache);
		request.setAttribute("requestData", requestData);

		return "jtqy/manager";
	}

	/**
	 * 签约详情页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/xyManager.html")
	public String xyManager(HttpServletRequest request) {
        String JGXZQHDM = QuickPrincipalUtils.getXzqhdm(request);
		Map map = RequestUtils.GetParameterMap(request);
		map.put("JGXZQHDM", JGXZQHDM);
		JSONObject data = jtqyService.initXyinfomaion(map);
		request.setAttribute("data", data);

		return "jtqy/xyManager";
	}
	
	@RequestMapping("/detail.html")
	public String detail(HttpServletRequest request)
	{
		Map map = RequestUtils.GetParameterMap(request);
		JSONObject data = jtqyService.cygxQuery(map);
		request.setAttribute("data", data);
		
		return "jtqy/" + map.get("to").toString();
	}
	
	@RequestMapping("/zjshDetail.html")
	public String zjshDetail(HttpServletRequest request)
	{
		Map map = RequestUtils.GetParameterMap(request);
		JSONObject data = jtqyService.cygxQuery(map);
		request.setAttribute("data", data);
		return "jtqy/zjshDetail";
	}
	/**
	 * 查询全部
	 * @return
	 */
	@RequestMapping("/query.html")
	public String query(HttpServletRequest request)
	{
        String USERID = QuickPrincipalUtils.getUserID(request);
        Map map = RequestUtils.GetParameterMap(request);
        map.put("USERID", USERID);
        map.put("TDJS", "1");
        map.put("TDZT", "1");
        JSONArray tdAry = jtqyService.findTdByUserId(map);
        JSONObject data = new JSONObject();
        data.put("tdAry", tdAry);
        request.setAttribute("data", data);
		return "jtqy/queryAll";
	}
	
	/**
	 * 签约查询
	 * @return
	 */
	@RequestMapping("/qycx.html")
	public String qycx(HttpServletRequest request)
	{
		Map map = ControllerCommonUtil.getQueryTdxxParams(request);
		JSONArray TDXX = jtqyService.queryTdxxAll(map);
		request.setAttribute("tdxxAry", TDXX);
		boolean isGLJG = true; //是管理机构
		if(QuickPrincipalUtils.getJglbdm(request).indexOf(GLJGLBDM) == -1){
			isGLJG = false;
		}
		request.setAttribute("isGLJG",isGLJG);
		request.setAttribute("PTJGID", QuickPrincipalUtils.getPtjgID(request));

		map.put("FWBZT", "1");
		map.put("JGXZQHDM", QuickPrincipalUtils.getXzqhdm(request));
		map.put("YLJGID",map.get("PTJGID"));
		JSONArray fwbAry = fwbService.queryAll(map);
		request.setAttribute("fwbAry", fwbAry);
		return "jtqy/qycx";
	}

	/**家庭签约协议*/
	@RequestMapping("/jtqyXy.html")
	public String jtqyxy(HttpServletRequest request){
		QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
		JSONObject userInfo = principal.getUserInfo();

		JSONObject json = new JSONObject();
		//json.put("YFXM", userInfo.getString("XM"));
		//json.put("YFSJHM", userInfo.getString("SJHM"));
		json.put("YFYLJGMC", userInfo.getString("YLJGMC"));
		json.put("JGXZQHMC",userInfo.getString("JGXZQHMC"));
		request.setAttribute("requestData", json);
		return "jtqy/jtqyXy";
	}

	/** 转介、变更、续约、解约---协议变更 */
	@RequestMapping("/changeXy.html")
	public String changeXy(HttpServletRequest request){
		QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
		JSONObject userInfo = principal.getUserInfo();

		JSONObject json = new JSONObject();
		json.put("YFXM", userInfo.getString("XM"));
		json.put("YFSJHM", userInfo.getString("SJHM"));
		json.put("YFYLJGMC", userInfo.getString("YLJGMC"));
		json.put("JGXZQHMC",userInfo.getString("JGXZQHMC"));
		request.setAttribute("requestData", json);
		return "jtqy/changeXy";
	}
	
	/**
	 * 家庭签约协议_服务包详细
	 * @param request
	 * @return
	 */
	@RequestMapping("/jtqyXyFwbDetail.html")
	public String jtqyXyFwbDetail(HttpServletRequest request){
		String FWBID = request.getParameter("FWBID");
		if(!StringUtils.isEmpty(FWBID)){
			JSONArray ary = fwbFwxxDAO.queryFwxxsByFwbId(FWBID);
			request.setAttribute("fwxAry", ary);
		}
		return "jtqy/jtqyXy_fwbDetail";
	}
	
	/**
	 * 在线打印协议
	 * @param request
	 * @return
	 */
//	@RequestMapping("/printXyOnline.html")
//	public String printXyOnLine(HttpServletRequest request){
//		Map<String, String> map = RequestUtils.GetParameterMap(request);
//		if(!map.isEmpty()){
//			JSONArray xyData = qyxxDAO.queryPrintXyByQyxxids(map);
//			request.setAttribute("xyData", xyData);
//		}
//		return "jtqy/printXy_online";
//	}

	/**
	 * 电子协议列表页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/indexDzxy.html")
	public String indexDzxy(HttpServletRequest request) {
		return "jtqy/indexDzxy";
	}

	/**
	 * 协议详情页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryXyDetail.html")
	public String queryXyDetail(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		String XYID = map.get("XYID");
		if(StringUtils.isEmpty(XYID)){
			JSONObject xyObj = qysjbDao.queryLast(map);
			request.setAttribute("XYID",xyObj.get("XYID"));
		}else{
			request.setAttribute("XYID", XYID);
		}
		request.setAttribute("QYXXID",map.get("QYXXID"));
		return "jtqy/queryXyDetail";
	}

	/**
	 * 选择医保服务项
	 * @param request
	 * @return
	 */
	@RequestMapping("/selectYbfwx.html")
	public String selectYbfwx(HttpServletRequest request) {
		request.setAttribute("JGXZQHDM", QuickPrincipalUtils.getXzqhdm(request));
		return "jtqy/select_ybfwx";
	}
}
